<?php

use PaypalPayoutsSDK\Core\PayPalHttpClient;
use PaypalPayoutsSDK\Core\ProductionEnvironment;
use PaypalPayoutsSDK\Core\SandboxEnvironment;
use PaypalPayoutsSDK\Payouts\PayoutsItemGetRequest;

require __DIR__  . '/vendor/autoload.php';

error_reporting(E_ALL);

// 回调类型
function sendBack($data, $msg='', $code=200){
    $backinfo = json_encode([
        'code'=> $code,
        'msg' => $msg,
        'data'=> $data,
    ]);
    echo $backinfo;
    exit();
}

// Creating an environment
$clientId = "AVQ68-wx9VQrSNHfJTg2PWTWr37_6-nE-XRQV6pf2wro3ZUG70Wt29qaeQHKhGldyE55AsGgRZykzNQN";
$clientSecret = "EPnwkDY6xOUDCRJP0dBA9KeB8ckxAndYmE7djY7ZpUbistKPaCFQZV7HLDGqlIK7WZF1SK0CcXYYdc2l";
define('SANDBOX', true);


if(SANDBOX){
    $environment = new SandboxEnvironment($clientId, $clientSecret);
} else {
    $environment = new ProductionEnvironment($clientId, $clientSecret);
}

$jsonBody = json_decode(file_get_contents("php://input"), true);
$itemId = trim(strval($jsonBody['payout_item_id']));

try {
    $client = new PayPalHttpClient($environment);

    $request = new PayoutsItemGetRequest($itemId);
    $response = $client->execute($request);
} catch (Exception $e) {
    sendBack(['error'=>$e->getMessage()], '出现异常啦~', 400);
}

if(isset($response->result->errors)) {
    sendBack(['error'=>$response->result->errors], '出错啦~', 404);
}
sendBack($response->result, 'payout success~', 200);